package org.anddev.andengine.entity.particle;

import org.anddev.andengine.entity.particle.emitter.IParticleEmitter;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

/**
 * 
 * @author adam
 *
 * Helper particle system that can be triggered for a set number of loops then turn itself off,
 * handy for short explosions
 *
 */

public class TriggeredParticleSystem extends ParticleSystem {

	private Boolean mTriggered = false;
	private int mLoops = 0;

	public TriggeredParticleSystem(final IParticleEmitter pParticleEmitter, final float pMinRate, final float pMaxRate, final int pMaxParticles, final TextureRegion pTextureRegion) {
		super(pParticleEmitter,pMinRate,pMaxRate,pMaxParticles,pTextureRegion);
	}

	@Override
	protected void onManagedUpdate(final float pSecondsElapsed) {
		if (mTriggered==true) {
			if (mLoops < 1) {
				setParticlesSpawnEnabled(false);
				mTriggered = false;
			} else {
				mLoops--;
			}
		}
		super.onManagedUpdate(pSecondsElapsed);
	}
	
	@Override
	/*
	 * work as normal particle system
	 */
	public void setParticlesSpawnEnabled(boolean pParticlesSpawnEnabled) {
		mLoops = 0;
		mTriggered = false;
		super.setParticlesSpawnEnabled(pParticlesSpawnEnabled);
	}
	
	/*
	 * trigger for a set number of loops (engine update frames)
	 */
	public void setParticlesSpawnEnabled(boolean pParticlesSpawnEnabled, final int pLoops) {
		mLoops = pLoops;
		mTriggered = true;		
		super.setParticlesSpawnEnabled(pParticlesSpawnEnabled);
	}	
	
	/*
	 * check if particle system is still active
	 */
	public boolean canKill() {
		return (mTriggered == false && this.mParticlesAlive<1);
	}
}
